import { createRouter, createWebHashHistory } from 'vue-router'
import home from '@/views/homeView.vue'
import { useCounterStore } from '@/stores/counter'
import { Dialog } from 'vant'
const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      name: 'home',
      component: home,
    },
    {
      path: '/family',
      name: 'family',
      component: () => import('@/views/familyView.vue'),
    },
    {
      path: '/note',
      name: 'note',
      component: () => import('@/views/noteView.vue'),
    },
    {
      path: '/photo',
      name: 'photo',
      component: () => import('@/views/photoView.vue'),
    },
    {
      path: '/message',
      name: 'message',
      component: () => import('@/views/messageView.vue'),
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/loginView.vue'),
    },
    {
      path: '/search',
      name: 'search',
      component: () => import('@/views/searchView.vue'),
    },
    {
      path: '/article',
      name: 'article',
      component: () => import('@/views/articleList.vue'),
      children: [
        {
          path: '/article',
          redirect: '/articleClass',
        },
        {
          path: '/articleClass',
          name: 'articleClass',
          component: () => import('@/views/articles/articleClass.vue'),
        },
        {
          path: '/hobbyPart',
          name: 'hobbyPart',
          component: () => import('@/views/articles/hobby-part.vue'),
        },
        {
          path: '/studyMore',
          name: 'studyMore',
          component: () => import('@/views/articles/studyMore.vue'),
        },
      ],
    },
    {
      path: '/studyPath',
      name: 'studyPath',
      component: () => import('@/views/studyPath.vue'),
    },
    {
      path: '/user',
      name: 'user',
      component: () => import('@/views/userView.vue'),
      meta: { requiresAuth: true },
    },
    {
      path: '/detail',
      name: 'detail',
      component: () => import('@/views/detailView.vue'),
    },
    {
      path: '/weather',
      name: 'weather',
      component: () => import('@/views/tools/weatherView.vue'),
    },
    {
      path: '/AI',
      name: 'AI',
      component: () => import('@/views/AiView.vue'),
    },
    {
      path: '/:catchAll(.*)',
      name: 'error',
      component: () => import('@/views/errorView.vue'),
    },
  ],
})

router.beforeEach(async (to, from, next) => {
  const counter = useCounterStore()
  const token = localStorage.getItem('TTOO')
  if (to.meta.requiresAuth) {
    if (token) {
      try {
        const userStatus = await counter.loginedStatusFn(token)
        // console.log('登录状态', userStatus)
        if (userStatus) {
          next()
        } else {
          Dialog.confirm({
            title: '提示',
            message: '您登录状态失效，是否跳转到登录页面？',
          })
            .then(() => {
              router.push('/login')
            })
            .catch(() => {
              router.push('/')
            })
        }
      } catch (error) {
        console.log(error)
        next(false)
      }
    } else {
      // 没有token
      Dialog.confirm({
        title: '提示',
        message: '您还未登录，是否跳转到登录页面？',
      })
        .then(() => {
          router.push('/login')
        })
        .catch(() => {
          router.push('/')
        })
    }
  } else {
    next()
  }
})

export default router
